import { _decorator, Component, Node, Label, director, tween, UITransform, UIOpacity, Tween } from 'cc';
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';
import AdManager from 'db://assets/Script/Manager/AdManager';
import SSS_GameEventManager, { SS_GameEventData } from 'db://assets/Script/Manager/SSS_GameEventManager';
import { SSS_Consts } from 'db://assets/Script/Data/SSS_Consts';
import { HexagonJenga_GameData } from '../Data/HexagonJenga_GameData';

const { ccclass, property } = _decorator;

@ccclass('HexagonJenga_GamePanel')
export default class HexagonJenga_GamePanel extends Component {
    static Instance: HexagonJenga_GamePanel;

    @property(Label)
    public Label_level: Label | null = null;

    @property(Node)
    Button_back: Node | null = null;
    @property(Node)
    Button_refresh: Node | null = null;

    // @property(Node)
    // Button_撤回: Node | null = null;
    // @property(Node)
    // Button_加瓶子: Node | null = null;

    // @property(Label)
    // Label_撤回_number: Label | null = null;
    // @property(Node)
    // 撤回广告icon: Node | null = null;

    // @property(Label)
    // Label_加瓶子_number: Label | null = null;
    // @property(Node)
    // 加瓶子广告icon: Node | null = null;

    // @property(Label)
    // public Label_数量上限提示: Label | null = null;

    onLoad() {
        HexagonJenga_GamePanel.Instance = this;
    }

    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    Refresh() {
        // this.Label_level.string = "第 " + HexagonJenga_GameData.Get_level() + " 关";

        // this.Label_撤回_number.string = `${DaoShuiPaiXu_GameStorage.data.mode_1.item_撤回_number}`;
        // this.Label_加瓶子_number.string = `${HexagonJenga_GameData.Get_item_加瓶子_number()}`;

        // this.撤回广告icon.active = DaoShuiPaiXu_GameStorage.data.mode_1.item_撤回_number <= 0;
        // this.加瓶子广告icon.active = HexagonJenga_GameData.Get_item_加瓶子_number() <= 0;
    }
    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);

        // this.Button_撤回.on(Node.EventType.TOUCH_END, this.On_Button_撤回, this);
        // this.Button_加瓶子.on(Node.EventType.TOUCH_END, this.On_Button_加瓶子, this);
    }
    RemoveListener() {
        this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.off(Node.EventType.TOUCH_END, this.On_Button_refresh, this);

        // this.Button_撤回.off(Node.EventType.TOUCH_END, this.On_Button_撤回, this);
        // this.Button_加瓶子.off(Node.EventType.TOUCH_END, this.On_Button_加瓶子, this);
    }

    On_Button_back() {
        // SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Menu);
        director.loadScene(SSS_Consts.SceneName.MainScene);

        SSS_AudioManager.PlayButtonAudio();
    }

    On_Button_refresh() {
        director.loadScene(SSS_Consts.SceneName.HexagonJenga_Game_Scene);

        SSS_AudioManager.PlayButtonAudio();
    }

    On_Button_撤回() {
        // DaoShuiPaiXu_GameManager.Instance.撤回();

        // if (DaoShuiPaiXu_GameStorage.data.mode_1.item_撤回_number > 0) {
        //     DaoShuiPaiXu_GameStorage.data.mode_1.item_撤回_number--;
        //     DaoShuiPaiXu_GameStorage.saveData();
        //     this.Refresh();

        //     DaoShuiPaiXu_GameManager.Instance.撤回();
        // }
        // else {
        //     AdManager.是否看完广告 = false;
        //     AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
        //         () => {
        //             director.emit(SS_GameEventData.广告结束);

        //             this.增加道具撤回次数();
        //         },
        //         () => {
        //             director.emit(SS_GameEventData.广告结束);

        //             console.log("广告没看完！");
        //         },
        //     );
        // }

        SSS_AudioManager.PlayButtonAudio();
    }

    On_Button_加瓶子() {
        // if (CupMgr.ins.curCfg.length >= Global.maxTube) {

        //     this.Label_数量上限提示.node.active = true;
        //     let opacity = this.Label_数量上限提示.node.getComponent(UIOpacity);
        //     opacity.opacity = 255;

        //     Tween.stopAllByTarget(opacity);
        //     tween(opacity)
        //         .delay(1)
        //         .to(1, { opacity: 0 })
        //         .call(() => {
        //             this.Label_数量上限提示.node.active = false;
        //             opacity.opacity = 0;
        //         })
        //         .start();
        // }

        // if (!CupMgr.ins.checkCanAddTube()) return;

        // if (HexagonJenga_GameData.Get_item_加瓶子_number() > 0) {
        //     HexagonJenga_GameData.Set_item_加瓶子_number(HexagonJenga_GameData.Get_item_加瓶子_number() - 1);
        //     this.Refresh();

        //     DaoShuiPaiXu_GameManager.Instance.加瓶子();
        // }
        // else {
        //     AdManager.是否看完广告 = false;
        //     AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
        //         () => {
        //             director.emit(SS_GameEventData.广告结束);

        //             this.增加道具加瓶子次数();
        //         },
        //         () => {
        //             director.emit(SS_GameEventData.广告结束);

        //             console.log("广告没看完！");
        //         },
        //     );
        // }

        // SSS_AudioManager.PlayButtonAudio();
    }

    增加道具撤回次数() {
        // DaoShuiPaiXu_GameStorage.data.mode_1.item_撤回_number += HexagonJenga_GameData.item_撤回_add_number;
        // DaoShuiPaiXu_GameStorage.saveData();
        // this.Refresh();
    }
    增加道具加瓶子次数() {
        HexagonJenga_GameData.Set_item_加瓶子_number(HexagonJenga_GameData.Get_item_加瓶子_number() + HexagonJenga_GameData.item_加瓶子_add_number);
        this.Refresh();
    }
}

// /** 调试 */
window["HexagonJenga_GamePanel"] = HexagonJenga_GamePanel;